# Author: didiplus
# Date: 2025-01-22 15:47:36
# LastEditTime: 2025-01-22 15:47:39
# LastEditors: didiplus
# Description: 
# FilePath: \pythonscript\Captcha\utls\TextCaptcha.py
from .CaptchaBase import CaptchaBase
import random
from PIL import Image, ImageDraw

class TextCaptcha(CaptchaBase):
    """文本验证码"""

    def generate(self):
        """生成随机文本验证码"""
        char_pool = {
            self.TYPE_DEFAULT: "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789",
            self.TYPE_ONLY_NUMBER: "0123456789",
            self.TYPE_ONLY_CHAR: "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz"
        }
        chars = char_pool.get(self.char_type, char_pool[self.TYPE_DEFAULT])
        self.chars = "".join(random.choices(chars, k=self.length))

    def create_image(self) -> Image.Image:
        """生成验证码图像"""
        if self.chars is None:
            self.generate()

        image = Image.new("RGB", (self.width, self.height), (255, 255, 255))
        draw = ImageDraw.Draw(image)

        if self.font is None:
            self.set_font()

        for i, char in enumerate(self.chars):
            x = 10 + i * (self.width - 20) // self.length
            y = random.randint(0, self.height // 3)
            draw.text((x, y), char, font=self.font, fill=self.random_color())
           
        # 添加干扰空心圆
        self.draw_hollow_circle(draw, num_circles=5, min_radius=2, max_radius=5)
        self.draw_bezier_curve(draw, num_curves=1)
        return image
